package com.wavehi.response_deal.config;

import com.wavehi.response_deal.converter.CustomerMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Author WaveHi
 * @Date 6/11/2021 下午 10:13
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Configuration
public class MyConfig{

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {

            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                HashMap<String, MediaType> h = new HashMap<>();
                h.put("xml", MediaType.APPLICATION_XML);
                h.put("json", MediaType.APPLICATION_JSON);
                h.put("x-myData", MediaType.parseMediaType("application/x-myData"));
                ParameterContentNegotiationStrategy pcns = new ParameterContentNegotiationStrategy(h);
                HeaderContentNegotiationStrategy header = new HeaderContentNegotiationStrategy();
                configurer.strategies(Arrays.asList(pcns, header));
            }

            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new CustomerMessageConverter());
            }
        };
    }

}
